package ch.uzh.ifi.sonar.qacpp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import ch.uzh.ifi.sonar.util.SonarConstant;

public class StreamAnalysisResultWriter {

	public final static String CSV_SEP = SonarConstant.CSV_SEP; // as
																// Schindler's
																// default
																// setting
	// in Windows OS

	private final BufferedWriter writer;

	public StreamAnalysisResultWriter(final BufferedWriter writer) {
		this.writer = writer;
	}

	public void writeHeader() throws IOException {
		writer.write("Year");
		writer.write(CSV_SEP);
		writer.write("Module");
		writer.write(CSV_SEP);
		writer.write("Sub Module");
		writer.write(CSV_SEP);
		writer.write("File");
		writer.append(CSV_SEP);
		writer.write("STCYC");
		writer.append(CSV_SEP);
		writer.write("STDIT");
		writer.append(CSV_SEP);
		writer.write("STGTO");
		writer.append(CSV_SEP);
		writer.write("STLCM");
		writer.append(CSV_SEP);
		writer.write("STLIN");
		writer.append(CSV_SEP);
		writer.write("STMIF");
		writer.append(CSV_SEP);
		writer.write("STMTH");
		writer.append(CSV_SEP);
		writer.write("STNOC");
		writer.append(CSV_SEP);
		writer.write("STNOP");
		writer.append(CSV_SEP);
		writer.write("STOPT");
		writer.append(CSV_SEP);
		writer.write("STSUB");
		writer.append(CSV_SEP);
		writer.write("STTLN");
		writer.append(CSV_SEP);
		writer.write("STVAR");
		writer.append(CSV_SEP);
		writer.write("STWMC");
		writer.append(CSV_SEP);
		writer.write("STXLN");
		writer.append(System.getProperty("line.separator"));
	}

	public void writeContent(MetFileAnalyzer metAnalyzer) throws IOException {
		/* Write the module */
		String sourceFile = metAnalyzer.getSourceFileName();
		// from the second slash, we count the module
		int firstSlash = MetFileAnalyzer.getSubStrIndex(1, "\\", sourceFile);
		String module = firstSlash == -1 ? "" : sourceFile.substring(0,
				firstSlash);
		writer.append("Year");// add year
		writer.append(CSV_SEP);
		writer.write(module);
		writer.append(CSV_SEP);
		/* Write the sub module */
		int secondSlash = MetFileAnalyzer.getSubStrIndex(1, "\\", sourceFile);
		String subModule = secondSlash == -1 ? "" : sourceFile.substring(
				firstSlash + 1, secondSlash);
		writer.write(subModule);
		writer.append(CSV_SEP);
		writer.append(sourceFile);
		writer.append(CSV_SEP);
		writer.append(Long.toString(metAnalyzer.getSTCYC()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTDIT()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTGTO()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTLCM()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTLIN()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTMIF()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTMTH()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTNOC()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTNOP()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTOPT()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTSUB()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTTLN()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTVAR()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTWMC()));
		writer.append(CSV_SEP);
		writer.write(Long.toString(metAnalyzer.getSTXLN()));
		writer.append(System.getProperty("line.separator"));
	}

	public void writeSumFromDetailFiles(File[] files) {
		for (File file : files) {
			FileInputStream fis;
			try {
				fis = new FileInputStream(file.getAbsolutePath());
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(fis));
				int lineCount = 0;
				String line;
				while ((line = reader.readLine()) != null) {
					lineCount++;
					if( lineCount > 1){
						writer.write(line);
						writer.write(System.getProperty("line.separator"));
					}
				}

			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public void close() throws IOException {
		writer.close();
	}
}